﻿using LBCMS.EntityFrameWork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using LBCMS.Helper;

namespace LBCMS.Controllers
{
    public class apiController : Controller
    {
        BLLContext db = new BLLContext();
        Helper.JsonHelper.retDataSingle<string> ret = new Helper.JsonHelper.retDataSingle<string>();

        [HttpPost]
        public ActionResult login(string loginID, string password)
        {
            return Content(Helper.JsonHelper.toJson(new BLL.login().getSession(loginID, password)));
        }


        /// <summary>
        /// 注销
        /// </summary>
        /// <returns></returns>
        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }


        /// <summary>
        /// 用户头像上传
        /// </summary>
        /// <param name="userGuid"></param>
        /// <returns></returns>
        public ActionResult userImgUpload()
        {
            if (Request.Files.Count == 0)
            {
                ret.success = false;
                ret.msg = "未检测到上传文件！";

            }
            else
            {
                var file = Request.Files[0];
                string path = "Upload/UserFace/" + DateTime.Now.ToString("yyyyMMddHHmmss") + file.FileName;
                file.SaveAs(Server.MapPath("~") + "\\" + path);
                var userGuid = Session["userGuid"].ToString();
                //更新用户
                var user = db.B_Users.FirstOrDefault(p => p.userGuid == userGuid);
                if (user != null)
                {
                    user.userFace = path;
                    db.SaveChanges();
                }
                ret.data = path;
                ret.success = true;
                ret.msg = "上传成功！";
            }

            return Content(JsonHelper.toJson(ret));
        }
    }
}